/***
 ** 广告定价 校验
 **/

define(['../model/data'], function(DATA) {
    //budgetVal, input node, budgetType
    return function(budgetVal, node, _bType) {
        //var mode = node.parents("[data-price]").data("price");//CPC,CPM,OCPM,CPA
        //rm mode ,replace with billingEvent
        var gIndex = node.parents("div.one-dist[index]").attr("loc-index"); //todo verify ,
        var locIndex = node.closest(".single-price").attr("index");
        var info = DATA.getBidInfoVo(gIndex, locIndex);
        if ('' == budgetVal) {
            //如果预算值清空，则不再显示警告
            rmWarning(node);
            info.budget = 0;
            return false;
        }
        var topmost = 1000000;
        var billingEvent = info.billingEvent;
        //var optimizationGoal=DATA.prices[gIndex].optimizationGoal;
        //这个版本只用一个出价，这一个就是max
        var max = Number(info.sBidAmount);
        //页面输入的预算
        budgetVal = Number(budgetVal);
        //每日最小预算
        var minDailyBudget = getMinDailyBudget(max, billingEvent);
        var type = _bType; //0 日预算，1总预算
        //去除预算限制 2016-07-08
        // if(type==0){
        //     //日预算
        //     if(budgetVal<minDailyBudget||budgetVal>topmost){
        //         var warning;
        //         if(budgetVal>topmost){
        //             warning= I18N.getText('createAd', 'dailyBudgetMaxLimit', '100W');
        //         }else if(minDailyBudget==5&&billingEvent!="IMPRESSIONS"){
        //             warning= I18N.getText('createAd', 'dailyBudgetMinLimit', '5');
        //         }else if(minDailyBudget>5&&billingEvent!="IMPRESSIONS"){
        //             warning= I18N.getText('createAd', 'dailyBudgetMinPriceLimit', '*5='+minDailyBudget);
        //         }else if(minDailyBudget==1&&billingEvent=="IMPRESSIONS"){
        //             warning= I18N.getText('createAd', 'dailyBudgetMinLimit','1');
        //         }else if(minDailyBudget>1&&billingEvent=="IMPRESSIONS"){
        //             warning= I18N.getText('createAd', 'dailyBudgetMinPriceLimit', '*1='+minDailyBudget);
        //         }
        //         addWarning(node,warning);
        //     }else{
        //         rmWarning(node);
        //         info.budget=budgetVal;
        //     }
        // }else if(type==1){
        //     //总预算
        //     var scheduleday =$('#period-date').data('daterangepicker'); //todo verify
        //     var day = calculateScheduleDay(scheduleday.startDate, scheduleday.endDate);
        //     var totalBid=acc(minDailyBudget,day);
        //     var top = topmost * day; // 总预算 排程天数 * $1,000,000
        //     if(budgetVal<totalBid||budgetVal>top){
        //         var warning;
        //         if(budgetVal>top){
        //             warning= I18N.getText('createAd', 'lifetimeBudgetMaxLimit', '100W$') +  (day == 1 ? '' : '*' + I18N.getText('createAd', 'scheduleDays'));
        //         }else if(minDailyBudget==5&&billingEvent!="IMPRESSIONS"){
        //             warning= I18N.getText('createAd', 'lifetimeBudgetMinLimit', [5,totalBid]);
        //         }else if(minDailyBudget>5&&billingEvent!="IMPRESSIONS"){
        //             warning= I18N.getText('createAd', 'lifetimeBudgetMinPriceLimit', ['5$*',totalBid]);
        //         }else if(minDailyBudget==1&&billingEvent=="IMPRESSIONS"){
        //             warning= I18N.getText('createAd', 'lifetimeBudgetMinLimit', [1,totalBid]);
        //         }else if(minDailyBudget>1&&billingEvent=="IMPRESSIONS"){
        //             warning= I18N.getText('createAd', 'lifetimeBudgetMinPriceLimit', ['',totalBid]);
        //         }
        //         addWarning(node,warning);
        //     }else{
        //         rmWarning(node);
        //         info.budget=budgetVal;
        //     }
        // }
    };

    //添加警告显示
    function addWarning(node, msg) {
        node.addClass("error");
        node.next("div.tooltip").find("div.tooltip-inner").text(msg);
        node.next("div.tooltip").css("opacity", 1);
    }
    //移除警告显示
    function rmWarning(node) {
        node.removeClass("error");
        node.next("div.tooltip").css("opacity", 0);
    }
    //根据出价和结算方式计算每日的最小预算
    function getMinDailyBudget(maxPrice, billingEvent) {
        var bid;
        if (maxPrice < 1) maxPrice = 1;
        if (billingEvent == "IMPRESSIONS") {
            //CPM OR OCPM
            bid = Number(maxPrice).toFixed(2);
        } else {
            //CPC OR CPA
            bid = Number(5 * maxPrice).toFixed(2);
        }
        return bid;
    }

    function acc(arg1, arg2) {
        //if(arg1==undefined||arg2==undefined) return  '';
        //var m=0,s1=arg1.toString(),s2=arg2.toString();
        //try{m+=s1.split(".")[1].length}catch(e){}
        //try{m+=s2.split(".")[1].length}catch(e){}
        //return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m)
        return Number(arg1 * arg2).toFixed(2);
    }

    function calculateScheduleDay(_startDate, _endDate) {
        var scheduleDays = (_endDate - _startDate) / 1000 / 60 / 60 / 24;
        var scheduleDaysT = Number(scheduleDays).toFixed(0);
        if (scheduleDays > scheduleDaysT) {
            scheduleDays = Number(scheduleDaysT) + 1;
        } else {
            scheduleDays = Number(scheduleDaysT);
        }

        return scheduleDays;
    }
});
